
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WriteXMLFile {

	public static void main(String argv[]) {

		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("backup");
			doc.appendChild(rootElement);

			// staff elements
			Element server = doc.createElement("Server");
			rootElement.appendChild(server);



			// set attribute to staff element
			//Attr attr = doc.createAttribute("id");
			//attr.setValue("1");
			//server.setAttributeNode(attr);


			// set attribute to staff element
			//Attr attr = doc.createAttribute("id");
			//attr.setValue("1");
			//staff.setAttributeNode(attr);

			// shorten way
			server.setAttribute("server", "ch00sm33");

		//	Element type1 = doc.createElement("type");
		//	rootElement.appendChild(type1);

		//	Element type = doc.createElement("type");
		//	type.appendChild(doc.createTextNode("Savegroup Summary"));
		//	type1.appendChild(type);

			Element savegroup = doc.createElement("savegroup");
			savegroup.appendChild(doc.createTextNode("H_Logs_SPS"));
			server.appendChild(savegroup);

			Element duration = doc.createElement("duration");
			duration.appendChild(doc.createTextNode("00:31:01 (HH:MM:SS)"));
			server.appendChild(duration);	

			Element size = doc.createElement("size");
			size.appendChild(doc.createTextNode("202.7 MB"));
			server.appendChild(size);	

			Element sg_count = doc.createElement("savegroupcount");
			sg_count.appendChild(doc.createTextNode("67"));
			server.appendChild(sg_count);	

			Element startTime = doc.createElement("StartTime");
			startTime.appendChild(doc.createTextNode("Mon Jun 24 20:15:00 2013"));
			server.appendChild(startTime);	

			Element endTime = doc.createElement("EndTime");
			endTime.appendChild(doc.createTextNode("Mon Jun 24 20:46:01 2013"));
			server.appendChild(endTime);

			Element total = doc.createElement("total");
			total.appendChild(doc.createTextNode("5"));
			server.appendChild(total);	

			Element succeeded = doc.createElement("clientsucceeded");
			succeeded.appendChild(doc.createTextNode("0"));
			server.appendChild(succeeded);

			Element failed = doc.createElement("clientfailed");
			failed.appendChild(doc.createTextNode("1"));
			server.appendChild(failed);

			Element clientSucceeded = doc.createElement("clientSucceeded");
			clientSucceeded.appendChild(doc.createTextNode("ch00sa183, ch00sa184, ch00sa186.extranet.mt.com, ch00sa200, ch00sm200"));
			server.appendChild(clientSucceeded);	

			Element disabled = doc.createElement("clientdisabled");
			disabled.appendChild(doc.createTextNode("ch00sa190,ch00sa191"));
			server.appendChild(disabled);





			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File("d:\\file.xml"));

			// Output to console for testing
			//StreamResult result = new StreamResult(System.out);

			transformer.transform(source, result);

			System.out.println("File saved!");

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}
	}
}